package zjc.bookkeeping.minepage.cClassificationmanagement;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import zjc.bookkeeping.R;
import zjc.bookkeeping.activity.MainActivity;
import zjc.bookkeeping.db.ConsumptionClassBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.minepage.dAccountbookmanagement.AddbookDialog;
import zjc.bookkeeping.tools.ToolsClass;

public class AddDialog extends Dialog implements View.OnClickListener {
    ConsumptionClassBean consumptionClassBean = new ConsumptionClassBean();
    TextView title,name;
    RadioGroup radioGroup;
    RadioButton oneBtn,manyBtn;
    EditText nameEt;
    Button sureBtn,cancelBtn;
    public AddDialog(@NonNull Context context) {
        super(context);
    }
    public interface AddSure{
        void onSureListener();
    }
    AddSure addSure;

    public void setAddSure(AddSure addSure) {
        this.addSure = addSure;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_account);
        initView();
    }
    private void initView() {
        title = findViewById(R.id.textView3);
        title.setText("请选择消费分类类型");
        name = findViewById(R.id.textView4);
        name.setText("分类名：");
        radioGroup = findViewById(R.id.dialog_account_rg);
        oneBtn = findViewById(R.id.dialog_account_one);
        oneBtn.setText("支出");
        manyBtn = findViewById(R.id.dialog_account_many);
        manyBtn.setText("收入");
        nameEt = findViewById(R.id.dialog_account_name);
        nameEt.setHint("请输入分类名。");
        sureBtn = findViewById(R.id.dialog_account_sure_btn);
        cancelBtn = findViewById(R.id.dialog_account_cancel_btn);

        sureBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        radioGroup.check(oneBtn.getId());
        consumptionClassBean.setConsumption_class_kind(0);
        consumptionClassBean.setConsumption_class_image("outcome_other");
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = findViewById(i);
                if (radioButton.getText().toString().equals("单人账本")){
                    consumptionClassBean.setConsumption_class_kind(0);
                    consumptionClassBean.setConsumption_class_image("outcome_other");
                }else {
                    consumptionClassBean.setConsumption_class_kind(1);
                    consumptionClassBean.setConsumption_class_image("income_other");
                }
            }
        });
    }

            @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_account_cancel_btn:
                cancel();
                break;
            case R.id.dialog_account_sure_btn:
                if (nameEt.getText().toString().isEmpty()){
                    ToolsClass.showToast(getContext(),"请输入账本名.", Toast.LENGTH_SHORT);
                    return;
                }else {
                    consumptionClassBean.setConsumption_class_name(nameEt.getText().toString());
                        DBManager.insertConsumptionClass(consumptionClassBean);
                    addSure.onSureListener();
                    }
                cancel();
                break;
                    }
    }
    public void setDialogSize(){
        Window window =getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = (int) (display.getWidth());
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);
    }
}
